$(function () {
    //检查登录状态
    $.post("checkLogin",function (data) {
        //请求服务端是否等处于登录状态
        //{"id": ,"name": ,"password": ,"money": ,"head": ,}
        if (data!=null){
            //已登录
            $("#userName").text(data.name);
            $("#userHead").attr("src",data.head);
            //点击自己的头像可以进入一个个人界面,查看订单和处理的订单

        }else {
            //未登录 .点击头像即可进如login.html
            $("#userName").attr("href","login.html");
        }
    },"json");


    var html = $("#userName").html();
    $.post("findAllShoppingCar",function (data) {
        console.log(data);
        for (var i=0;i<data.length;i++){
            $("#articleTr").append("<tr id="+data[i].articleId+">\n" +
                "                <td><input type='checkbox' name='check'></td>\n" +
                "                <td>"+(i+1)+"</td>\n" +
                "                <td>"+data[i].articleName+"</td>\n" +
                "                <td>\n" +
                "                    <span>"+data[i].number+"</span>\n" +
                "                    <button type='button' class='btn btn-default btn-xs' onclick=add("+data[i].articleId+")>\n" +
                "                        <span class='glyphicon glyphicon-chevron-up' style='color: rgb(255, 140, 60);'></span>\n" +
                "                    </button>\n" +
                "                    <button type='button' class='btn btn-default btn-xs' onclick='less("+data[i].articleId+")'>\n" +
                "                        <span class='glyphicon glyphicon-chevron-down' style='color: rgb(255, 140, 60);'></span>\n" +
                "                    </button>\n" +
                "                </td>\n" +
                "                \n" +
                "                <td>"+data[i].money+"</td>\n" +
                "            </tr>")
        }

    },"json");
});

function paySelected() {
    //支付订单
    // var names = document.getElementsByName("check");
    var check = $("#articleTr input:checked"); //可以看成是数组,
    //parent() 获取当前对象的父节点对象
    var array="";
    for (var i=0;i<check.length;i++){
        var parent = $(check[i]).parent().parent().attr("id");
        //获取所有的id值后发送给后端
        array=array.concat(parent+"#")

    }
    console.log(array);
    $.post("payOrder",{"list":array},function () {
        location.href = "shoppingCart.html";
    });


}
//增加计数  id也是商品的id值
function add(id) {
    console.log(id);
    //数量dom对象
    var $1 = $("#"+id +" td:nth-child(4) span:first");
    //金额dom对象
    var $2 = $("#"+id +" td:nth-child(5)");
    var html = $1.html();

    $.post("addCount",{"count":html,"id":id},function (data) {
        //{newCount : ,money: ,msg: }
        var newCount = data.newCount;
        var money = data.money;
        $1.html(newCount);
        $2.html(money)

    },"json");

}

//减少计数
function less(id) {
    //数量dom对象
    var $1 = $("#"+id +" td:nth-child(4) span:first");
    //金额dom对象
    var $2 = $("#"+id +" td:nth-child(5)");
    var html = $1.html();

    $.post("lessCount",{"count":html,"id":id},function (data) {
        //{newCount : ,money:  }
        var newCount = data.newCount;
        var money = data.money;
        $1.html(newCount);
        $2.html(money)


    },"json");
}

//删除选中
function del() {
    var names = document.getElementsByName("check");
    for (var i=0;i<names.length;i++){
        if (names[i].checked){
            //parentNode 获取父节点
            var temp = names[i].parentNode.parentNode;
            var id = $(temp).attr("id");//商品id值
            $.post("delOrder",{"id":id},function () {
                //删除订单
                temp.remove();
            });
        }
    }
}

//全选
function selectAll(){
    var all = document.getElementById("all");
    var names = document.getElementsByName("check");
    for (var i=0;i<names.length;i++){
        names[i].checked = all.checked;
    }
}
//退出登录
function out() {
    $.post("outLogin",function () {
        location.href = "shop_index.html";
    });
}